#!/usr/bin/env python
""" Module to send an email via gmail.
	You need to supply a username and password,
	a recipient, subject and some text.
	Usage:
	mail("sender@gmail.com", "password",
	["recipient1@aol.com","recipient2@yahoo.com"], 
	"a subject", "some text")
"""

import smtplib
from email.MIMEMultipart import MIMEMultipart
from email.MIMEBase import MIMEBase
from email.MIMEText import MIMEText
from email.Utils import COMMASPACE, formatdate
from email import Encoders
import os

def mailer(user, passwd, to, subject, text):
	"""Main function to send an email"""

	gmail_user = user
	gmail_pwd = passwd
	msg = MIMEMultipart()
	msg['To'] = COMMASPACE.join(to)
	msg['Subject'] = subject
	msg.attach(MIMEText(text))
	mailServer = smtplib.SMTP("smtp.gmail.com", 587)
	mailServer.ehlo()
	mailServer.starttls()
	mailServer.ehlo()
	mailServer.login(user, passwd)
	mailServer.sendmail(user, to, msg.as_string())
	mailServer.close()

def main():
	mail()


if __name__ == '__main__':
	main()

